﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    // Layout = null;
    // var url = Context.Request.GetEncodedPathAndQuery();
    // string d = "";
    //var url = Request.RawUrl;
}
@section css {
    <style>
        .less-query .more { display: none; }
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) { height: 34px; line-height: 34px; padding: 0 8px; }
    </style>
} 
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 5px;">
                    <form class="layui-form layui-form-pane less-query" id="frmSearch" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">关键字</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="keyword" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline more">
                                <label class="layui-form-label">用户姓名</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="username" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline more">
                                <label class="layui-form-label">用户性别</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="sex" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline more">
                                <label class="layui-form-label">用户城市</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="city" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline more">
                                <label class="layui-form-label">用户职业</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="classify" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon layui-icon-search"></i> 搜 索</button>
                                <button type="button" class="layui-btn layui-btn-primary" id="btnMoreQuery">
                                    <i class="layui-icon layui-icon-down"></i>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> <i class="layui-icon layui-icon-add-1"></i>添加 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> <i class="layui-icon layui-icon-delete"></i>删除 </button>
                </div>
            </script> 
            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="currentTableBar">
                <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
                <a class="layui-btn layui-btn-xs layui-bg-cyan data-count-permission" lay-event="permission"><i class="layui-icon layui-icon-set-fill"></i>角色权限</a>
            </script>
        </div>
    </div>
</div>
 
@section js {
    <script>
        var baseUrl = '@(Url.Content("~/"))';
        layui.use(['form', 'table','miniAdmin','tree'], function () {
            var $t = layui.jquery;
            var table = layui.table;
            var tree = layui.tree;
            var form = layui.form;
            var admin = layui.miniAdmin;
            //var getTreeData 
            $("#btnMoreQuery").on("click", function () {
                var t = $(this)
                var frmSearch = $("#frmSearch");
                var icon = t.find('i');
                if (icon.hasClass('layui-icon-up')) {
                    icon.removeClass('layui-icon-up').addClass('layui-icon-down');
                    frmSearch.addClass("less-query").removeClass('more-query');
                } else {
                    icon.addClass('layui-icon-up').removeClass('layui-icon-down');
                    frmSearch.addClass("more-query").removeClass('less-query');
                }
                 $(".table-search-fieldset").trigger("resize");
            });

             var mainTable = table.render({
                id:"roleMng",
                elem: '#currentTableId',
                url: '@(Url.Content("~/sys/role/page"))',
                method:'post',
                parseData: function (res) {
                     
                    return {
                        "code": res.resultCode, //解析接口状态
                        "msg": res.message, //解析提示文本
                        "count": res.data.count, //解析数据长度
                        "data": res.data.items //解析数据列表
                    };
                },
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                height: function () {
                    var height = $(".table-search-fieldset").height();
                    // 69 为各个 margin 的和
                    var resultHeight = $(window).height() - height - 35;
                    if (resultHeight < 300) {

                        return 'auto';
                    }
                    return resultHeight;
                }, // 最大高度减去其他容器已占有的高度差
                cols: [[
                    { type: "checkbox", width: 40 },
                    { field: 'name', width: 80, title: '用户名', sort: true },
                    { field: 'createTime', title: '创建时间', width: 150, sort: true },
                    { field: 'updateTime', width: 180, title: '最后更新时间', sort: true },
                    { title: '操作', width: 220, toolbar: '#currentTableBar', align: "left" }
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                autoSort:false, // 服务端排序
                response: {
                    statusCode: 200 // 重新规定成功的状态码为 200，table 组件默认为 0
                }
            });


            $(window).on('resize',function(){
                mainTable.resize();
            });
            $(".table-search-fieldset").on('resize',function(){
                // console.log('.table-search-fieldset resize:', $(".table-search-fieldset").height())
                mainTable.resize();
            });

            // 触发排序事件
            table.on('sort(currentTableFilter)', function (obj) {

                var where = obj.config.where;
                where.SortField = obj.field

                if (where.SortField) {
                    where.SortType = obj.type
                }
                var option = table.getOptions('roleMng');
                //执行搜索重载
                table.reload('roleMng', {
                    initSort: obj, // 记录初始排序，如果不设的话，将无法标记表头的排序状态。
                    where: where
                }, 'data');
            });
            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                //var result = JSON.stringify(data.field);
                // layer.alert(result, {
                //     title: '最终的搜索信息'
                // });
                // 获取配置项
                var $option = table.getOptions('roleMng');
                // console.log('获取配置项', $option);
                // 复制搜索条件
                var where = JSON.parse(JSON.stringify(data.field));
                if ($option.where && $option.where.SortField) {
                    where.SortField = $option.where.SortField
                    where.SortType = $option.where.SortType
                }
                //执行搜索重载
                table.reload('roleMng', {
                    page: {
                        curr: 1
                    },
                    where: where
                }, 'data');

                return false;
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加角色',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '@(Url.Content("~/sys/role/edit/"))',
                        end: function(e){
                          
                          // 紧紧重新加载数据
                          table.reloadData('roleMng');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') {  // 监听删除操作
                     var checkStatus = table.checkStatus(obj.config.id)
                            , data = checkStatus.data;
                    if(!data || data.length == 0 ){
                        layer.msg('请选择需要删除的数据！');
                        return ;
                    }
                    layer.confirm('真的删除行么', function (index) { 
                       // BatDelete
                        var ids =  data.map(d=>d.id).join(",")
                         $.ajax({
                                url:  '@(Url.Content("~/sys/role/BatDelete"))',
                                data: {ids : ids},
                                method:'post',
                                success: function (d) {
                                    console.log(d);
                                    // l.destroy();
                                    layer.close(index);
                                    if (d.resultCode == "200") {

                                        table.reload('roleMng');

                                    } else {

                                        layer.msg(d.message);

                                        layer.close(index);
                                    }
                                    // alert(d)
                                },
                                error: function (e) {
                                    console.log(e);
                                     layer.close(index);
                                }
                            });
                    });
                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                // console.log(obj)
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                // console.log('数据', data, obj);
                if (obj.event === 'edit') {

                    var index = layer.open({
                        title: '编辑角色',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '@(Url.Content("~/sys/role/edit/"))' + data.id,
                        end: function(e){
                          //console.log('关闭编辑');
                          // 紧紧重新加载数据
                          table.reloadData('roleMng');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    // 删除
                    layer.confirm('真的删除行么', function (index) {
                        obj.del();
                         $.ajax({
                            url:  '@(Url.Content("~/sys/role/delete/"))' + data.id,
                            data: data,
                            method:'post',
                            success: function (d) {
                                // console.log(d);
                                // l.destroy();
                                layer.close(index);
                                if (d.resultCode == "200") {
                                 
                                    table.reload('roleMng');

                                } else {

                                    layer.msg(d.message);

                                    layer.close(index);
                                }
                                // alert(d)
                            },
                            error: function (e) {
                                console.log(e);
                                layer.close(index);
                            }
                        });
                       
                    });
                }else if(obj.event ==='permission') {
                    // 角色权限
                     //console.log('角色权限',data,obj);
                     $.ajax({
                            url:  '@(Url.Content("~/sys/role/GetRoleMenus"))',
                            data: data,
                            method:'post',
                            success: function (d) {
                                var menuIds = d.data.menuIds
                                var menutree = admin.getTreeData(d.data.menus,'id','parentId',menuIds);
                                var roleTreeId = 'roletree'
                                var divRoleLayerId = layer.open({
                                  type: 1, // page 层类型
                                  area: ['500px', '600px'],
                                  title: '角色权限',
                                  shade: 0.6, // 遮罩透明度
                                  shadeClose: false, // 点击遮罩区域，关闭弹层
                                  maxmin: false, // 允许全屏最小化
                                  anim: 0, // 0-6 的动画形式，-1 不开启
                                  content: '<div style="padding: 32px;"><div id="divroletree"/></div>',
                                  btn: ['确定', '关闭'],
                                  success:function (index, layero, that){
                                    // 渲染
                                    var roleTree = tree.render({
                                        elem: '#divroletree',
                                        data: menutree,
                                        showCheckbox: true,  // 是否显示复选框
                                        onlyIconControl: true,  // 是否仅允许节点左侧图标控制展开收缩
                                        id: roleTreeId,
                                        // isJump: true, // 是否允许点击节点时弹出新窗口跳转
                                        customName:{
                                            id: 'id',
                                            title: 'name',
                                        }
                                    });
                                  },
                                  btn1: function (index, layero, that) {
                                     var defer = $.Deferred(); 
                                     var check_data = tree.getChecked(roleTreeId);
                                     var ids = admin.getTreeCheckedIds(check_data,'id');
                                     // console.log(roleTreeId + ' getChecked',check_data,ids);
                                     $.ajax({
                                        url:  '@(Url.Content("~/sys/role/SaveRoleMenus"))',
                                        data: {
                                            roleId:data.id,
                                            menusIds:ids.join(',')
                                        },
                                        method:'post',
                                        success: function (d) {
                                            // console.log(d);
                                            if(d.resultCode !='200'){
                                               admin.error(d.message);
                                            }else{
                                               layer.close(divRoleLayerId);
                                            }
                                        }
                                     });
                                  },
                                  btn2: function (index, layero, that) {
                                        var defer = $.Deferred(); 
                                  },
                                }); 
                            },
                            error: function (e) {
                                console.log(e);
                                layer.close(index);
                            }
                        });
                }
            });
        });
    </script>
}